package org.lilred.ui;

import org.lilred.ui.theme.Theme;
import org.lilred.ui.theme.Themeable;

import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.component.TextField;

public class DecoratedInputField extends Manager implements Themeable {
	Theme defaultTheme;
	Theme focusTheme;
	int padding = 0;
	TextField textField;
	
	public DecoratedInputField(TextField field, Theme defaultTheme, Theme focusTheme) {
		this(field, 0, defaultTheme, focusTheme);
	}
	
	public DecoratedInputField(TextField field, int padding, Theme defaultTheme, Theme focusTheme) {
		super(0);
		this.textField = field;
		add(field);
		this.padding = padding;
		this.defaultTheme = defaultTheme;
		this.focusTheme = focusTheme;
	}
	
	protected void onFocus(int direction) {
		super.onFocus(direction);
		invalidate();
	}
	
	protected void onUnfocus() {
		super.onUnfocus();
		invalidate();
	}
	
	protected void paint(Graphics g) {
		if (isFocus()) getFocusTheme().paint(getWidth(), getHeight(), g);
		else getDefaultTheme().paint(getWidth(), getHeight(), g);
		super.paint(g);
	}
	
	public int getPreferredHeight() {
		return textField.getFont().getHeight() + 2 * padding;
	}
	
	public int getPreferredWidth() {
		return Integer.MAX_VALUE; 
	}
	
	protected void sublayout(int width, int height) {
		layoutChild(textField, getWidth() - 2 * padding, textField.getFont().getHeight());
		setPositionChild(textField, padding, padding);
		setExtent(width, getPreferredHeight());
	}

	public Theme getActiveTheme() {
		return null;
	}

	public Theme getDefaultTheme() {
		return defaultTheme;
	}

	public Theme getFocusTheme() {
		// TODO Auto-generated method stub
		return focusTheme;
	}
}
